<template>
  <div style="width: 1000px; margin: 0 auto">
    <h1>注册页面</h1>
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      :rules="rules"
      label-width="auto"
    >
      <el-form-item label="手机号" prop="userMobile">
        <el-input v-model="ruleForm.userMobile" />
      </el-form-item>
      <el-form-item label="手机号验证码" required>
        <el-col :span="11">
          <el-form-item prop="phoneCode">
            <el-input v-model="ruleForm.phoneCode" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col class="text-center" :span="2">
          <span class="text-gray-500"></span>
        </el-col>
        <el-col :span="11">
          <el-form-item prop="date2">
            <el-button
              type="danger"
              @click="submitFormSendPhoneCode(ruleFormRef)"
              >获取手机号验证码</el-button
            >
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="密码" prop="userPass">
        <el-input v-model="ruleForm.userPass" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          注册
        </el-button>
        <el-button @click="resetForm(ruleFormRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script lang="ts" setup>
import { reactive, ref } from "vue";
import { sendPhoneCode, getReg } from "../api/api";
import { ElMessage, type FormInstance, type FormRules } from "element-plus";
import { useRouter } from "vue-router";
import { push } from "echarts/types/src/component/dataZoom/history.js";
const router = useRouter();
interface RuleForm {
  name: string;
  region: string;
  count: string;
  date1: string;
  date2: string;
  delivery: boolean;
  location: string;
  type: string[];
  resource: string;
  desc: string;
}

const ruleFormRef = ref<FormInstance>();
const ruleForm = ref({
  userMobile: "",
  userPass: "",
  phoneCode: "",
});

const locationOptions = ["Home", "Company", "School"];

const rules = reactive<FormRules>({
  userMobile: [
    { required: true, message: "请输入手机号", trigger: "blur" },
    {
      pattern:
        /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
      message: "手机号格式不正确",
      trigger: "blur",
    },
  ],
  userPass: [
    { required: true, message: "请输入密码", trigger: "blur" },
    {
      pattern: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{3,10}$/,
      message: "密码必须由大小写字母，数字组成，长度必须在3-10位",
      trigger: "blur",
    },
  ],
  phoneCode: [
    { required: true, message: "请输入手机号验证码", trigger: "blur" },
    {
      min: 4,
      max: 4,
      message: "手机号验证码必须为4位数",
      trigger: "blur",
    },
  ],
});

const submitFormSendPhoneCode = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validateField(["userMobile"], (valid) => {
    if (valid) {
      console.log("submit!");
      sendPhoneCode(ruleForm.value.userMobile).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("发送手机号验证码成功");
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      console.log("error submit!");
    }
  });
};
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      getReg(ruleForm.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("注册成功");
          router.push("/login");
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}));
</script>
